<?php namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Services\AdminManager;
use App\Plan;
use App\PlanJoin;
use App\PlanSetNum;
use App\ShareInfo;
use Illuminate\Http\Request;
use Auth;

class PlanController extends Controller
{
    public function setPlan(Request $request){
        $title     = $request->get('title');
        $author    = $request->get('author');
        $amount    = $request->get('amount');
        $selectNum = $request->get('select_num');
        $inventNum = $request->get('invent_num');
        $startTime = $request->get('start_time');
        $endTime   = $request->get('end_time');
        $selectTime= $request->get('select_time');
        $content   = $request->get('content');
        $freeContent = $request->get('free_content');
        $upNum       = $request->get('up_num');
        $id          = $request->get('id');
        $videoId     = $request->get('video_id');
        $bannerId    = $request->get('banner_id');
        $desc        = $request->get('desc');
        $planNum     = !is_null($request->get('plan_num')) ?$request->get('plan_num') : 10000;

        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereId($id)->first();

        if(!$thatPlan){
            $thatPlan = new Plan();
        }

        if(!is_null($title))$thatPlan->title = $title;
        if(!is_null($author))$thatPlan->author = $author;
        if(!is_null($amount))$thatPlan->amount = $amount;
        if(!is_null($selectNum))$thatPlan->select_num = $selectNum;
        if(!is_null($inventNum))$thatPlan->invent_num = $inventNum;
        if(!is_null($startTime))$thatPlan->start_time = $startTime;
        if(!is_null($endTime))$thatPlan->end_time = $endTime;
        if(!is_null($selectTime))$thatPlan->select_time = $selectTime;
        if(!is_null($content))$thatPlan->content = $content;
        if(!is_null($freeContent))$thatPlan->free_content = $freeContent;
        if(!is_null($upNum))$thatPlan->up_num = $upNum;
        if(!is_null($videoId))$thatPlan->video_id = $videoId;
        if(!is_null($bannerId))$thatPlan->banner_img_id = $bannerId;
        if(!is_null($desc))$thatPlan->desc = $desc;
        if(!is_null($planNum))$thatPlan->plan_num = $planNum;
        $thatPlan->save();

        return response()->json(['level'=>'success','message'=>'设置或更新完成','data'=>$thatPlan]);
    }

    public function startPlan(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $planId = $request->get('plan_id');

        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereId($planId)->first();
        if(!$thatPlan)
            return response()->json(['level'=>'error','message'=>'不存在该计划']);

        $hasStartPlan = Plan::whereStatus(1)->first();
        if($hasStartPlan){
            $hasStartPlan->status = 0;
            $hasStartPlan->save();
        }

        if(!$hasStartPlan || ($hasStartPlan && $hasStartPlan->id != $planId)){
            $thatPlan->status = 1;
            $thatPlan->save();

            return response()->json(['level'=>'success','message'=>'启动完成','data'=>$thatPlan]);
        }

        return response()->json(['level'=>'success','message'=>'关闭完成','data'=>$thatPlan]);
    }

    public function delPlan(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Plan $plan */
        $plan = Plan::whereId($id)->first();

        if(!$plan)
            return response()->json(["level"=>"error","message"=>"该地址不存在！"],203);

        $plan->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }

    public function setContact(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var PlanJoin $thatPlanJoin */
        $thatPlanJoin = PlanJoin::whereId($id)->first();

        if(!$thatPlanJoin)
            return response()->json(["level"=>"error","message"=>"数据不存！"]);

        $thatPlanJoin->is_contact = 1;
        $thatPlanJoin->save();

        return response()->json(['level'=>'success','message'=>'标记完成']);
    }

    public function setSelect(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var PlanJoin $thatPlanJoin */
        $thatPlanJoin = PlanJoin::whereId($id)->first();

        if(!$thatPlanJoin)
            return response()->json(["level"=>"error","message"=>"数据不存！"]);

        if($thatPlanJoin->status == 2){
            return response()->json(["level"=>"error","message"=>"该用户资格已被取消！"]);
        }

        $thatPlanJoin->is_select = 1;
        $thatPlanJoin->save();

        return response()->json(['level'=>'success','message'=>'抽选完成']);
    }

    public function setClose(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var PlanJoin $thatPlanJoin */
        $thatPlanJoin = PlanJoin::whereId($id)->first();

        if(!$thatPlanJoin)
            return response()->json(["level"=>"error","message"=>"数据不存！"]);

        if($thatPlanJoin->status != 2){
            $thatPlanJoin->is_select = 0;
            $thatPlanJoin->status    = 2;
            $thatPlanJoin->save();
        }else{
            $thatPlanJoin->status = 1;
            $thatPlanJoin->save();
        }


        return response()->json(['level'=>'success','message'=>'设置完成']);
    }

    public function setPlanNum(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $minNum = $request->get('min');
        $maxNum = $request->get("max");
        $second = $request->get("second");

        if((int)$maxNum <= (int)$minNum)
            return response()->json(['level'=>'error','message' => "最大值必须大于最小值"]);

        if((int)$minNum < 0)
            return response()->json(['level'=>'error','message' => "最小值最小为0"]);

        if((int)$maxNum < 1)
            return response()->json(['level'=>'error','message' => "最大值不能小于1"]);

        $thatPlanSet = PlanSetNum::whereId(1)->first();
        if(!$thatPlanSet)
            $thatPlanSet = new PlanSetNum();

        $thatPlanSet->min_num = $minNum;
        $thatPlanSet->max_num = $maxNum;
        $thatPlanSet->second  = $second;
        $thatPlanSet->save();

        return response()->json(['level'=>'success','message'=>'设置完成']);
    }

    public function planSetAction(){
        $thatPlanSet = PlanSetNum::whereId(1)->first();
        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereStatus(1)->first();

        if($thatPlanSet && $thatPlan && $thatPlanSet->second > 0){
            $num = rand($thatPlanSet->min_num, $thatPlanSet->max_num);

            if(time() >= (strtotime($thatPlan->updated_at) + (int)$thatPlanSet->second)){
                $thatPlan->invent_num = $num + $thatPlan->invent_num;
                $thatPlan->save();
            }
        }

        return response()->json(["e"=>"success"]);
    }

    public function setShareInfo(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $title = $request->get('title');
        $desc  = $request->get('desc');
        $url   = $request->get('url');
        $imgId = $request->get('img_id');

        /** @var ShareInfo $thatShare */
        $thatShare = ShareInfo::wherePage("home")->first();
        if($thatShare){
            $thatShare->title = $title;
            $thatShare->desc  = $desc;
            $thatShare->url   = $url;
            $thatShare->img_id = $imgId;
            $thatShare->save();
        }

        return response()->json(['level'=>'success','message'=>'设置完成']);
    }
}